//
//  STDSMixMaskEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSMixMaskEffect_h
#define STDSMixMaskEffect_h
#include "STDSBaseEffect.hpp"

class STDSMixMaskEffect: public STDSBaseEffect {
public:
    STDSMixMaskEffect() {
        mEffectType = STDSEffectTypeMixMask;
    }
    ~STDSMixMaskEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp("opacity", name) == 0) {
            mOpacity = *floatValue;
        }
    }
    
    void setStringArtuments(const char *name, const char *stringValue) override {
        if (strcmp("inputImageTexture2", name) == 0) {
            mDSTPath = string(stringValue);
            mDSTText = nullptr;
        } else if (strcmp("maskTexture", name) == 0) {
            mMaskPath = string(stringValue);
            mMaskTex = nullptr;
        }
    }
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mMixMaskProgram == nullptr) {
            mMixMaskProgram = mContext->getProgram(kShaderSRCMixMask);
        }
        return mMixMaskProgram;
    }
    
    shared_ptr<STDSTexture> getMask() {
        if (mMaskTex == nullptr) {
            mMaskTex = mContext->getImageTexture(mMaskPath);
        }
        return mMaskTex;
    }
    
    shared_ptr<STDSTexture> getDst() {
        if (mDSTText == nullptr) {
            mDSTText = mContext->getImageTexture(mDSTPath);
        }
        return mDSTText;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        auto mask = getMask();
        auto dst = getDst();
        
        if (program == nullptr || dst == nullptr || mask == nullptr || mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture1"), 0);
        
        program->use();
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, dst->getId());
        program->setUniform(program->getUniform("inputImageTexture2"), 0);
        
        program->use();
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, mask->getId());
        program->setUniform(program->getUniform("maskTexture"), 0);
        
        program->setUniform(program->getUniform("opacity"), mOpacity);
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    string mMaskPath{""}; // 蒙版路径
    string mDSTPath{""}; // 输入图2
    shared_ptr<STDSTexture> mMaskTex{nullptr};
    shared_ptr<STDSTexture> mDSTText{nullptr};
    float mOpacity{1.0};
    shared_ptr<STDSShaderProgram> mMixMaskProgram{nullptr};
};

#endif /* STDSMixMaskEffect_h */
